//
//  HHYStockCalculateVC.m
//  热沉温度计算器
//
//  Created by apple on 2019/4/16.
//  Copyright © 2019 apple. All rights reserved.
//

#import "HHYStockCalculateVC.h"
//VC
#import "GPResultViewC.h"


#define kInPlacehoderArr @[@"", @"元", @"元", @"%", @"%"]

@interface HHYStockCalculateVC ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *bgIV;
//bigSV
@property (nonatomic, strong) UIScrollView *bigSV;

@property (nonatomic, strong) UIView *contentView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftMargin;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightMargin;

@property (weak, nonatomic) IBOutlet UIButton *calculateBtn;

@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *inputTFArr;

@end

@implementation HHYStockCalculateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupSubViews];
    
//    [self setupSubViews];

    
}


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
//    //2.0
    self.bigSV.frame = CGRectMake(0, 0, hhy_kScreenWidth, hhy_kScreenHeight-hhy_kTabbarHeight-hhy_kStatusBarAndNavigationBarHeight);
    self.contentView.frame = CGRectMake(0, 0, hhy_kScreenWidth, 520);
    
}

- (void)createNavi {
    
    
    self.navigationItem.title = @"股票盈亏计算器";
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self createNavi];
    
}

- (void)setupSubViews {
    [self.view addSubview:self.bgIV];

    [self.view addSubview:self.bigSV];
    
    [self.bigSV addSubview:self.contentView];
    //
    
    if (@available(iOS 11.0, *)) {
        self.bigSV.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self updateUIAfterLoadNib];
    
    //隐藏键盘
    UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidekeyBoardAction)];
    [self.view addGestureRecognizer:tap];
}

#pragma mark - 创建rightView
- (UIView *)createRightViewWithTitle:(NSString *)title {
    
    if ([title isEqualToString:@""]) {
        return [[UIView alloc] initWithFrame:CGRectZero];
    }
    
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 28)];
    leftLabel.textColor = UIColor.lightGrayColor;
    leftLabel.font = [UIFont systemFontOfSize:16];
    leftLabel.textAlignment = NSTextAlignmentRight;
    leftLabel.text = title;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 28)];
    [view addSubview:leftLabel];
    return view;
}

#pragma mark - btnActions
- (void)hidekeyBoardAction {
    [self.view endEditing:YES];
}


- (void)updateUIAfterLoadNib {
    int i = 0;
    //输入框
    for (UITextField *tf in self.inputTFArr) {
        tf.rightViewMode = UITextFieldViewModeAlways;
        tf.rightView = [self createRightViewWithTitle:kInPlacehoderArr[i++]];
    
    }
    
    if (hhy_kScreenWidth > 375) {
        self.leftMargin.constant = 45.0f;
        self.rightMargin.constant = 45.0f;
    }
    
}

#pragma mark - 计算Action
- (void)calcuteAction {
    //检查是否全部有值
    BOOL validValue = YES;
    for (UITextField *tf in self.inputTFArr) {
        if ([HHYUtils isEmpty:tf.text]) {
            validValue = NO;
            break;
        }
    }
    if (!validValue) {
        [HHYUtils textExampleLong:@"请输入完整数据"];
        return;
    }
    
    NSMutableArray *temArr = [NSMutableArray arrayWithCapacity:self.inputTFArr.count];
    for (UITextField *tf in self.inputTFArr) {
        [temArr addObject:tf.text];
    }
    
    
    float I1 = [temArr[0] floatValue];
    float I2 = [temArr[1] floatValue];
    float I3 = [temArr[2] floatValue];
    float I4 = [temArr[3] floatValue];
    float I5 = [temArr[4] floatValue];
    
    //计算
    float R1 = I1 * I2;
    float R2 = I1 * I3;
    float R3 = (R2 - R1) - (R1*I4 + R2*I5)/100.0f;
    float R4 = R3/R1*100.0f;
    
    //建立Model
    GPModel *model = [GPModel new];
    model.createTime = [NSDate.date timeIntervalSince1970];
    model.input1 = [NSString stringWithFormat:@"%0.2f", I1];
    model.input2 = [NSString stringWithFormat:@"%0.2f", I2];
    model.input3 = [NSString stringWithFormat:@"%0.2f", I3];
    model.input4 = [NSString stringWithFormat:@"%0.2f", I4];
    model.input5 = [NSString stringWithFormat:@"%0.2f", I5];

    model.result1 = [NSString stringWithFormat:@"%0.2f", R1];
    model.result2 = [NSString stringWithFormat:@"%0.2f", R2];
    model.result3 = [NSString stringWithFormat:@"%0.2f", R3];
    model.result4 = [NSString stringWithFormat:@"%0.2f", R4];
    [model saveOrUpdate];
    
    [self pushToResultVCWithModel:model];

}

#pragma mark - 跳转至结果VC
- (void)pushToResultVCWithModel:(GPModel *)model {
    GPResultViewC *rc = [[GPResultViewC alloc] initWithGPModel:model];
    [self.navigationController pushViewController:rc animated:YES];
}

- (IBAction)btnActions:(UIButton *)btn {

    if (btn == self.calculateBtn) {
        [self hidekeyBoardAction];
        [self calcuteAction];
        return;
    }
    
}

#pragma mark - tapGestureAction
- (void)tapGestureAction:(UITapGestureRecognizer *)tap {
    if (@available(iOS 11.0, *)) {
        //防止多次触发事件
        __block UIView *blockView = tap.view;
        blockView.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            blockView.userInteractionEnabled = YES;
        });
    }
    
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    //2.0
//    if ([string isEqualToString:@"."]&&[textField.text containsString:@"."]) {
//        return NO;
//    }
    
//    //4.0
//    if ([HHYUtils isEmpty:textField.text] && [string isEqualToString:@"."]) {
//        return NO;
//    }

    return YES;
}

#pragma mark - getter
- (UIScrollView *)bigSV {
    if (!_bigSV) {
        _bigSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, hhy_kScreenWidth, hhy_kScreenHeight-hhy_kStatusBarAndNavigationBarHeight-hhy_kTabbarSafeBottomMargin)];
        //        mysssSV.delegate = self;
        //        _bigSV.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _bigSV.backgroundColor = [UIColor clearColor];
        _bigSV.showsHorizontalScrollIndicator = NO;
        _bigSV.showsVerticalScrollIndicator = NO;
        
        //contentSize
        _bigSV.contentSize = CGSizeMake(0, self.contentView.xl_height);
    }
    return _bigSV;
}


- (UIImageView *)bgIV {
    if (!_bgIV) {
        _bgIV = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _bgIV.image = [UIImage imageNamed:@"bg"];
    }
    return _bgIV;
}

- (UIView *)contentView {
    if (!_contentView) {
        UINib *nib = [UINib nibWithNibName:@"hhy_CalculateView" bundle:nil];
        NSArray *objs = [nib instantiateWithOwner:self options:nil];
        _contentView = objs[0];
        _contentView.frame = CGRectMake(0, 0, hhy_kScreenWidth, 520);
    }
    return _contentView;
}


@end
